package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
	@Autowired
	GeofenceMapper geofenceMapper;
	@Autowired
	VehicleMapper vehicleMapper;
	
	@Override
	public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
		log.debug("查询围栏业务,参数={}",geofenceQuery);
		List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
		for (int i = 0; i < list.size(); i++){
			GeofenceVO geofenceVO = list.get(i);
			VehicleQuery vehicleQuery = new VehicleQuery();
			vehicleQuery.setGeofenceId(geofenceVO.getId());
			List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
			int totalNum = vehicleVOList.size();
			int availableNum = 0;
			for (VehicleVO vehicleVO : vehicleVOList){
				if (vehicleVO.getStatus().equals("1")){
					availableNum++;
				}
			}
			geofenceVO.setTotalNum(totalNum);
			geofenceVO.setAvailableNum(availableNum);
			geofenceVO.setVehicleList(vehicleVOList);
		}
		return list;
	}
	@Override
	public void changeStatus(Long id, String status) {
		log.debug("修改围栏状态业务,参数{}",id,status);
		Geofence geofence = new Geofence();
		geofence.setId(id);
		geofence.setStatus(status);
		geofence.setUpdateTime(new Date());
		geofenceMapper.update(geofence);
		
		
	}
	
	@Override
	public void deleteGeofence(Long id) {
		log.debug("删除围栏业务,参数={}",id);
		VehicleQuery vehicleQuery = new VehicleQuery();
		vehicleQuery.setGeofenceId(id);
		List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
		if (list != null && list.size() > 0){
			throw new ServiceException(StatusCode.VEHICLE_EXISTS);
		}else{
			geofenceMapper.deleteById(id);
		}
		
		
		geofenceMapper.deleteById(id);
		
		
	}
	
	@Override
	public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
		log.debug("保存围栏业务,参数={}",geofenceSaveParam);
		// 创建围栏实体对象
		Geofence geofence = new Geofence();
		// 将参数对象的属性值复制到围栏实体对象中
		BeanUtils.copyProperties(geofenceSaveParam,geofence);
		// 设置围栏状态为"1"(启用状态)
		geofence.setStatus("1");
		// 设置创建时间
		geofence.setCreateTime(new Date());
		// 设置更新时间
		geofence.setUpdateTime(new Date());
		// 调用mapper保存围栏信息
		geofenceMapper.insert(geofence);
	}
}
